var GameUiTools = {

    /**
     * 按钮添加事件监听
     * cc.Component.EventHandler 类型
     * 属性：
     *     target: Node 目标节点
     *     component: String 目标组件名
     *     handler: String 响应事件函数名
     *     customEventData: String 自定义事件数据
     */
    setButtonClickEvents: function(component, menu, handler, isScale, customEventData) {
        let arrayMenu = new Array();
        if (menu.length == undefined) {
            arrayMenu[0] = menu;
        } else {
            arrayMenu = menu;
        }
        for (let i = 0, len = arrayMenu.length; i < len; i++) {
            let clickEventHandler = new cc.Component.EventHandler();
            clickEventHandler.target = component.node; // 这个node节点是事件处理代码组件所属的节点
            clickEventHandler.component = component.node.name; 
            clickEventHandler.handler = handler;
            if (menu.length == undefined) {
                clickEventHandler.customEventData = customEventData;
            } else {
                clickEventHandler.customEventData = i;
            }
            let button = arrayMenu[i].addComponent(cc.Button);
            button.clickEvents.push(clickEventHandler);
            // 默认点击按钮，有缩放0.8的效果
            if (isScale == undefined || isScale) {
                button.transition = cc.Button.Transition.SCALE;
                button.duration = 0.1;
                button.zoomScale = 0.8;
            }
        }
    },

    /**
     * 加载场景
     * @param {*} sceneName : 场景名称
     */
    loadingScene(sceneName) {
        cc.director.preloadScene(sceneName, () => {
            cc.director.loadScene(sceneName);
        });
    },

    /**
     * 加载图层
     * @param {*} panelName ：图层路径
     */
    loadingLayer(panelName) {
        cc.loader.loadRes(panelName, (err, prefab) => {
            if (!err) {
                let node = cc.instantiate(prefab);
                cc.director.getScene().children[0].addChild(node);
            }
        });
    },

    /**
     * 移除图层
     */
    unLoadingLayer(node) {
        cc.director.getScene().children[0].removeChild(node);
    },

    /**
     * 元素移动动效
     */
    eleMove(node, pos) {
        node.setPosition(pos);
    }
};

export default GameUiTools;